
import turtle
import random
import time

turtle.pensize(1)  # 设置画笔大小，数值越小画出的线条越细
turtle.delay(100)  # 绘画延迟，单位：毫秒，数值越小绘画速度越快
turtle.hideturtle()  # 隐藏画笔
turtle.setup(800, 800)  # 设置画布大小


def star(x, y, left_angle, edge_len, color='yellow'):
    """
    画一个五角星
    :param x: 起始x坐标
    :param y: 起始y坐标
    :param left_angle: 画笔方向逆时针转动度数
    :param edge_len: 五角星边的长度
    :param color: 五角星颜色
    :return:
    """
    turtle.pencolor(color)  # ouline color of star
    turtle.fillcolor(color)  # filling color of star
    turtle.speed(0)  # drawing speed, 0 is the highest speed, accelerate from 1-10

    turtle.pu()  # raise the brush
    turtle.goto(x, y)  # move to the start location
    turtle.pd()  # put the brush down(ready to draw)

    turtle.begin_fill()  # start to fill the graph 
    turtle.left(left_angle)  # Brush direction with horizontal direction as the reference, rotate counterclockwise left_angle degree 
    for _ in range(5):  # Repeat to draw 5 edges
        turtle.forward(edge_len)  # Moves edge_len pixels in the direction of the current brush，drawing one edge of a star
        turtle.right(144)  # The brush is rotated 180 degrees clockwise. Since the inner angle of the star is 36 degrees, it needs to rotate 180-36 = 144 degrees
    turtle.end_fill()  # end of the filling
    turtle.left(-left_angle)  # Restore the brush direction to the horizontal direction, so as not to affect the subsequent drawing

# Drawing stars
turtle.bgpic('./bg.gif')
for _ in range(200):  # Draw 100 star
    # Randomly generate the starting coodinate, brush direction and star edge length 
    rand_x = random.randint(-400, 400)
    rand_y = random.randint(0, 400)
    edge_len = random.randint(3, 8)
    left_angle = random.randint(0, 180)
    star(rand_x, rand_y, left_angle, edge_len, '#B7C5D2')

turtle.done()